package grg.learn.compose.ui.components

import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.text.BasicText
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.sp
import grg.learn.compose.R
import kotlin.math.min

@Preview
@Composable
private fun preview() {
    Menus1(
        0,
        listOf(
            MenuData(
                painterResource(id = R.drawable.ic_launcher_background),
                "按钮1",
                Color.Cyan
            ),
            MenuData(
                painterResource(id = R.drawable.ic_launcher_background),
                "按钮2",
                Color.Blue
            )
        ),
        listOf(
            MenuData(
                painterResource(id = R.drawable.ic_launcher_foreground),
                "按钮1",
                Color.Red
            ),
            MenuData(
                painterResource(id = R.drawable.ic_launcher_foreground),
                "按钮2",
                Color.Green
            )
        )
    )
}


data class MenuData(
    val menuIcon: Painter,
    val text: String,
    val color: Color
)

/**
 * create by iso88591 at 2021/8/21 😤
 * 固定按钮组
 */
@Composable
fun Menus1(
    currentSelect: Int,
    menuSelect: List<MenuData>,
    menuUnSelect: List<MenuData>,
    modifierBtn: Modifier = Modifier,
    modifierText: Modifier = Modifier,
    fontSize: TextUnit = 15f.sp,
    selectChangeCall: ((position: Int, menuData: MenuData) -> Unit)? = null,
) {

    val size = min(menuSelect.size, menuUnSelect.size)

    if (size == 0) return

    var checkPosition by remember {
        mutableStateOf(currentSelect)
    }

    selectChangeCall?.invoke(currentSelect, menuSelect[currentSelect])

    Row(modifier = Modifier.fillMaxWidth()) {
        for (it in 0 until size) {
            val menuData =
                if (it == checkPosition) menuSelect[it]
                else menuUnSelect[it]

            Menu1(
                Modifier
                    .weight(1f)
                    .clickable {
                        checkPosition = it
                        selectChangeCall?.invoke(it, menuData)
                    },
                modifierBtn,
                modifierText,
                menuData,
                fontSize
            )
        }
    }

}

/**
 * create by iso88591 at 2021/8/21 😟
 * 按钮
 */
@Composable
fun Menu1(
    modifierBox: Modifier,
    modifierBtn: Modifier,
    modifierText: Modifier,
    menuData: MenuData,
    fontSize: TextUnit
) {
    Box(modifierBox, contentAlignment = Alignment.Center) {
        Column(Modifier.fillMaxWidth(), horizontalAlignment = Alignment.CenterHorizontally) {
            Image(
                painter = menuData.menuIcon,
                contentDescription = menuData.text,
                modifier = modifierBtn,
                contentScale = ContentScale.Fit
            )

            BasicText(
                text = menuData.text,
                style = TextStyle(
                    menuData.color,
                    fontSize = fontSize
                ),
                modifier = modifierText,
            )
        }
    }
}















